import Vue from 'vue'
import Vuex from 'vuex'

/* eslint-disable */

// 把vuex引入到vue中
Vue.use(Vuex)

const state = {
  count: 0
}

const mutations = { //同步修改
  add(state) {
    state.count++
  },
  reduce() {
    state.count--
  },
  addNum(state, num) {
    console.log(num);
    if (isNaN(num)) {
      num = num.num;
    }
    state.count += Number(num)
  }
}

const getters = { //调用改方法都会被调用，相当于过滤器
  count: state => {
    return state.count += 12.58;
  }
}

const actions = { //异步
  //可以调用 mutations中的方法
  addAction (context){
      context.commit('addNum',10)
      setTimeout(
        () =>{
          context.commit('reduce')
        },3000
      )
      console.log("我比reduce先执行")
  },
  reduceAction (context){
    context.commit('reduce')
  }
}

export default new Vuex.Store({
  state,
  mutations,
  getters,
  actions,
})
